#include "InternetOperator.h"


int createSockfd(char * ip,char * port){
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    int flag = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag));
    bind(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    return sockfd;
}

void addEpollNode(int epollfd,int fd){
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = fd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&event); 
}

void deleteEpollNode(int epollfd,int fd){
    epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
}

typedef struct {
    int length;
    char buf[1000];
}Train;

void transfile(int netfd){
    Train train;
    char * filename = "file1111.txt";
    train.length = strlen(filename);
    memcpy(train.buf,filename,train.length);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    int fd = open(filename,O_RDONLY);
    //加入进度条，传文件信息
    struct stat statbuf;
    fstat(fd,&statbuf);
    train.length = sizeof(statbuf);
    memcpy(train.buf,&statbuf,train.length);
    send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    char buf[1000];
    while(1){
        bzero(buf,sizeof(buf));
        ssize_t rdnum = read(fd,buf,sizeof(buf));
        train.length = rdnum;
        memcpy(train.buf,buf,rdnum);
        send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
        if(rdnum == 0){
            break;
        }
    }

     
}
